import GameUiTools from "GameUiTools";
import GameConfig from "GameConfig";
import GameTools from "GameTools";

cc.Class({
    extends: cc.Component,

    properties: {
        closeBtn: cc.Node,
        playAdBtn: cc.Node,
        playAgainBtn: cc.Node,
        homeBtn: cc.Node,
        rankBtn: cc.Node,
        shareBtn: cc.Node,
        playAdBox: cc.Node,
        scoreDisplay: cc.Label,
    },
    
    onLoad() {
        // 关闭按钮
        GameUiTools.setButtonClickEvents(this, this.closeBtn, "closeBtnFunc");
        // 回到首页
        GameUiTools.setButtonClickEvents(this, this.homeBtn, "backHomeFunc");
        // 再来一次
        GameUiTools.setButtonClickEvents(this, this.playAgainBtn, "playAgainFunc");
        // 排行榜按钮
        GameUiTools.setButtonClickEvents(this, this.rankBtn, "showRankFunc");
        // 分享按钮
        GameUiTools.setButtonClickEvents(this, this.shareBtn, "shareFunc");

        // 每局游戏的第一次失败 且微信小游戏环境 且基础库版本号 >= 2.0.4才能观看广告复活
        if (!GameConfig.CHANCE_USED && CC_WECHATGAME && wx.getSystemInfoSync().version >= "2.0.4") {
            this.playAdBox.active = true;
            // 观看广告
            GameUiTools.setButtonClickEvents(this, this.playAdBtn, "playAdFunc");
        }
    },

    init(game) {
        this.game = game;

        this.scoreDisplay.node.color = new cc.Color(254, 239, 13, 255);
        this.scoreDisplay.node.rotationX = 10;

        this.scoreDisplay.string = this.game.score.toString();
    },

    start() {
        // 最高分
        let score = this.game.score;
        // 游戏结束
        if (GameConfig.CURRENT_GAME_ID) {
            GameTools.ajax('/parkour/game/over', {
                gameId: GameConfig.CURRENT_GAME_ID,
                score: score
            }, function(data){}, function(data) {});
        }
        if (score > GameConfig.GAME_HIGHEST_SCORE) {
            GameConfig.GAME_HIGHEST_SCORE = score;
            GameTools.setItemByLocalStorage("GAME_HIGHEST_SCORE", score);
            // 提交分数
            GameTools.submitScore(score, GameConfig.MAIN_MENU_NUM);
        }
    },

    closeBtnFunc() {
        GameUiTools.loadingScene("MenuUI");
    },

    showRankFunc() {
        if (CC_WECHATGAME) {
            GameUiTools.loadingLayer("panel/Rank");
        } else {
            console.log("非微信小程序环境无法调起排行榜显示");
        }
    },

    playAdFunc() {
        //TODO 观看广告
        
        // 接着来
        GameConfig.CHANCE_USED = true;
        GameUiTools.unLoadingLayer(this.node);
        this.game.gameStart();
    },

    playAgainFunc() {
        GameConfig.CHANCE_USED = false;   // 复活机会是否已用掉
        GameUiTools.loadingScene("GameScene");
    },

    backHomeFunc() {
        GameConfig.CHANCE_USED = false;   // 复活机会是否已用掉
        GameUiTools.loadingScene("MenuUI");
    },

    shareFunc() {
        var _self = this;
        //主动拉起分享接口
        cc.loader.loadRes("shareIcon",function(err,data){
            let shareTitle = GameConfig.RESULT_SHARE_TITLE.replace('${score}', _self.game.score);
            wx.shareAppMessage({
                title: shareTitle,
                imageUrl: data.url,
                success(res){
                    GameTools.ajax('/parkour/game/share', {
                        title: shareTitle,
                        image: data.url,
                        query: 'shareCode='
                    }, function(data){}, function(data) {});
                },
                fail(res){
                    console.log("转发失败!!!")
                } 
            })
        });
    },
});
